#链接
[http://pat.zju.edu.cn/contests/pat-a-practise/1036]
#样例代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26n = int(raw_input())
male=[]
female=[]
for i in range(n):
(name,g,ids,grade) = (x for x in raw_input().split())
grade = int(grade)
if g =='M':
male.append([grade,name,ids])
else:
female.append([grade,name,ids])
male.sort()
female.sort()
if len(male)>0 and len(female)>0:
print female[len(female)-1][1],female[len(female)-1][2]
print male[0][1],male[0][2]
print female[len(female)-1][0] -male[0][0]
else:
if len(female) != 0:
print female[len(female)-1][1],female[len(female)-1][2]
else:
print "Absent"
if len(male) != 0:
print male[0][1],male[0][2]
else:
print "Absent"
print "NA"